package sige

import org.springframework.dao.DataIntegrityViolationException

class BimestreController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [bimestreInstanceList: Bimestre.list(params), bimestreInstanceTotal: Bimestre.count()]
    }

    
    
    def create(Long id) {
        def semestre = Semestre.get(id)
        semestre.bimestre_1 = new Bimestre()
        semestre.bimestre_2 = new Bimestre()
        [semestreInstance: semestre]
    }
    
    def save() {
        def bimestreInstance = new Bimestre(params)
        if (!bimestreInstance.save(flush: true)) {
            render(view: "create", model: [bimestreInstance: bimestreInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), bimestreInstance.id])
        redirect(action: "show", id: bimestreInstance.id)
    }

     def create_1(Long id) {
        def ano = AnoLetivo.get (id)
        def semestre = new Semestre(params)
        semestre.ano = ano
        [semestreInstance: semestre]
    }
    
    def save1(){
        def semestreInstance = new Semestre(params)
        
        semestreInstance.nome = "1° Semestre"
        semestreInstance.ano.semestre_1 = semestreInstance
        
        def semestreService = new SemestreService()
        semestreService.verificarDatas(semestreInstance)

        if(semestreInstance.hasErrors())
        {
            println(semestreInstance.errors)
            render(view: "create_1", model: [semestreInstance: semestreInstance])
            return
        }
        else if (!semestreInstance.save()) {
            render(view: "create_1", model: [semestreInstance: semestreInstance])
            return
        }
        
        def anoLetivoInstance = semestreInstance.ano
        
        anoLetivoInstance.save(flush: true)
        
        flash.message = message(code: 'default.created.message', args: [message(code: 'semestre.label', default: 'Semestre'), semestreInstance.id])
        redirect(controller: "bimestre", action: "create_1", id: semestreInstance.id ) 
    }
    
    def show(Long id) {
        def bimestreInstance = Bimestre.get(id)
        if (!bimestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), id])
            redirect(action: "list")
            return
        }

        [bimestreInstance: bimestreInstance]
    }

    def edit(Long id) {
        def bimestreInstance = Bimestre.get(id)
        if (!bimestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), id])
            redirect(action: "list")
            return
        }

        [bimestreInstance: bimestreInstance]
    }

    def update(Long id, Long version) {
        def bimestreInstance = Bimestre.get(id)
        if (!bimestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (bimestreInstance.version > version) {
                bimestreInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'bimestre.label', default: 'Bimestre')] as Object[],
                          "Another user has updated this Bimestre while you were editing")
                render(view: "edit", model: [bimestreInstance: bimestreInstance])
                return
            }
        }

        bimestreInstance.properties = params

        if (!bimestreInstance.save(flush: true)) {
            render(view: "edit", model: [bimestreInstance: bimestreInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), bimestreInstance.id])
        redirect(action: "show", id: bimestreInstance.id)
    }

    def delete(Long id) {
        def bimestreInstance = Bimestre.get(id)
        if (!bimestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), id])
            redirect(action: "list")
            return
        }

        try {
            bimestreInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'bimestre.label', default: 'Bimestre'), id])
            redirect(action: "show", id: id)
        }
    }
}
